 package kr.ac.skhu.m.controller.notice;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.util.PaggingUtil;
import kr.ac.skhu.m.util.StringUtil;
import kr.ac.skhu.m.value.NoticeListValue;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * 일반공지 컨트롤러
 * @author han
 *
 */
public class GeneralNoticeC extends AbstractController {
	
	private String mode;
	private String successView;
	
	public void setMode(String mode) {
		this.mode = mode;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = null;
		
		if(mode.equals("list")){
			mav = list(request, response);
		}else{
			mav = view(request, response);
		}
		
		return mav;
	}
	
	protected ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView(successView);
		
		String currentPage = ServletRequestUtils.getStringParameter(request, "curPage", "1");
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "1");
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord", "");
		
		String url = "http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10007&curpage=" + currentPage + 
				"&searchType=" + searchType + "&searchWord=" + searchWord;

		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();

		List<Element> board_list = source.getAllElementsByClass("left15");
		List<NoticeListValue> valueList = new ArrayList<NoticeListValue>();

		String text = "";
		String idx = "";

		String href = "";
		int start = 0; 
		int end = 0; 

		for(int i=0; i<board_list.size(); i++){
			Element element = board_list.get(i).getAllElements(HTMLElementName.A).get(0);
			text = element.getTextExtractor().toString();
			href = element.getAttributeValue("href");

			start = href.indexOf("idx=") + 4;
			end = href.indexOf("&");

			idx = href.substring(start, end);

			valueList.add(new NoticeListValue(text, idx));
		} 

		/**
		 * 페이징 정보
		 */
		//현재 페이지 번호
		Element cur = source.getAllElementsByClass("cur").get(0);
		String curPage = cur.getTextExtractor().toString();
		int intTypeCurPage = Integer.parseInt(curPage);
		
		//전체 페이지 수
		Element nextL = source.getAllElementsByClass("nextL").get(0);
		String nextLString = nextL.getAttributeValue("href");
		int nextLBegin = nextLString.indexOf("curpage=");
		int nextLEnd = nextLString.indexOf("&");
		String totalPage = nextLString.substring(nextLBegin + 8, nextLEnd);
		int intTypeTotalPage = Integer.parseInt(totalPage);
		
		PaggingUtil paggingUtil = new PaggingUtil(intTypeCurPage, intTypeTotalPage);
		
		mav.addObject("valueList", valueList);
		mav.addObject("paggingUtil", paggingUtil);
		mav.addObject("searchType", searchType);
		mav.addObject("searchWord", searchWord);
		
		return mav;
	}
	
	protected ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
		
		String currentPage = ServletRequestUtils.getStringParameter(request, "curPage", "1");
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "1");
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord", "");
		
		//송장 번호와 cj택배에 배송추적 관련 주소와 연결 시킨다.
		String url = "http://www.skhu.ac.kr/board/boardread.aspx?idx=" + request.getParameter("idx") + "&bsid=10007&curpage=" + currentPage;

		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();

		Element element = source.getAllElementsByClass("board_view_con").get(0).getAllElements(HTMLElementName.SPAN).get(0);

		String content = element.toString();
		 
		mav.addObject("content", content);
		mav.addObject("currentPage", currentPage);
		mav.addObject("searchType", searchType);
		mav.addObject("searchWord", searchWord);

		return mav;
	}
	
}
